﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections.Generic;
using HCMServices.DTO;
using System.Linq;
using System.Data.Linq;
using System.Data.Linq.Mapping;

namespace HCMServices.DAO
{
    /// <summary>
    /// This class process for TUKHOADIADIEM Table
    /// </summary>
    public class KeyPlaceNameDAO
    {
        private MyDataContext db;

        /// <summary>
        /// Constructor of class
        /// </summary>
        /// <param name="connectString">String indentify how to connect database</param>
        public KeyPlaceNameDAO(string connectString)
        {
            db = new MyDataContext(connectString);
        }

        /// <summary>
        /// This function find TUKHOADIADIEM rows base on TuKhoaDiaDiem column value
        /// </summary>
        /// <param name="strInput">Value of TuKhoaDiaDiem which want to find</param>
        /// <returns>List of TUKHOADIADIEM rows were found</returns>
        public List<KeyPlaceNameDTO> Search(string strInput)
        {
            List<KeyPlaceNameDTO> lstTuKhoaDiaDiemDTO = new List<KeyPlaceNameDTO>();

            IList<TUKHOADIADIEM> query = (from TUKHOADIADIEM tkdd in db.KeyPlaceName
                                          where strInput.IndexOf(tkdd.TuKhoaTenDiaDiem) > 0
                                          select tkdd).ToList();

            foreach (TUKHOADIADIEM tkdd in query)
            {
                KeyPlaceNameDTO temp = new KeyPlaceNameDTO();

                if (tkdd.MaTenDiaDiem != null)
                {
                    temp.KeyPlaceName = tkdd.TuKhoaTenDiaDiem;
                    temp.KeyPlaceNameId = tkdd.MaTuKhoaTenDiaDiem;
                    temp.PlaceNameId = (int)tkdd.MaTenDiaDiem;
                }
                else
                {
                    temp.KeyPlaceName = tkdd.TuKhoaTenDiaDiem;
                    temp.KeyPlaceNameId = tkdd.MaTuKhoaTenDiaDiem;
                }

                lstTuKhoaDiaDiemDTO.Add(temp);

            }

            return lstTuKhoaDiaDiemDTO;
        }

        /// <summary>
        /// This function find 10 rows of TUKHOADIADIEM Table base on value of TuKhoaDiaDiem column
        /// </summary>
        /// <param name="strInput">Value to find</param>
        /// <returns>List of 10 rows which was found first in database</returns>
        public List<KeyPlaceNameDTO> AutoCompleteTuKhoaDiaDiem(string strInput)
        {
            List<KeyPlaceNameDTO> lstTuKhoaDiaDiem = new List<KeyPlaceNameDTO>();

            IList<TUKHOADIADIEM> query = (from TUKHOADIADIEM tkdd in db.KeyPlaceName
                                          where tkdd.TuKhoaTenDiaDiem.IndexOf(strInput)>0
                                          select tkdd).Distinct().Take(10).ToList();

            foreach (TUKHOADIADIEM tkdd in query)
            {
                ExtractResult(lstTuKhoaDiaDiem, tkdd);
            }

            return lstTuKhoaDiaDiem;
        }

        private static void ExtractResult(List<KeyPlaceNameDTO> lstTuKhoaDiaDiem, TUKHOADIADIEM tkdd)
        {
            KeyPlaceNameDTO temp = new KeyPlaceNameDTO();
            if (null != tkdd.MaTenDiaDiem)
                temp.PlaceNameId = (int)tkdd.MaTenDiaDiem;
            temp.KeyPlaceNameId = tkdd.MaTuKhoaTenDiaDiem;
            temp.KeyPlaceName = tkdd.TuKhoaTenDiaDiem;

            lstTuKhoaDiaDiem.Add(temp);
        }

        /// <summary>
        /// This function search base on TuKhoaDiaDiem column of TUKHOADIADIEM Table and MaDichVu column of DICHVU Table
        /// </summary>
        /// <param name="strInput">Value of TuKhoaDiaDiem want to find</param>
        /// <param name="maDichVu">Value of MaDichVu want to find</param>
        /// <returns>List of 10 rows of TUKHOADIADIEM Table was found first</returns>
        public List<KeyPlaceNameDTO> AutoCompleteTuKhoaDiaDiemTheoDichVu(string strInput, int maDichVu)
        {
            List<KeyPlaceNameDTO> lstTuKhoaDiaDiem = new List<KeyPlaceNameDTO>();

            var result = (from tkdd in db.KeyPlaceName
                          from tendd in db.PlaceName
                          from dulieu in db.Data
                          where tkdd.TuKhoaTenDiaDiem.IndexOf(strInput) > 0 && tkdd.MaTenDiaDiem == tendd.MaTenDiaDiem
                                 && tendd.MaTenDiaDiem == dulieu.MaTenDiaDiem && dulieu.MaDichVu == maDichVu
                          select tkdd).Distinct();

            foreach (TUKHOADIADIEM tk in result.Take(10))
            {
                ExtractResult(lstTuKhoaDiaDiem, tk);
            }

            return lstTuKhoaDiaDiem;
        }

        public Dictionary<string, string> GetDictionaryOfKey()
        {
            Dictionary<string, string> result = new Dictionary<string, string>();

            var query = (from tkdd in db.KeyPlaceName
                         from tdd in db.PlaceName
                         where tkdd.MaTenDiaDiem == tdd.MaTenDiaDiem
                         select new { tkdd.TuKhoaTenDiaDiem, tdd.TenDiaDiem1 }).ToList();
            
            foreach (var t in query)
            {
                if(t.TuKhoaTenDiaDiem != null && !result.ContainsKey(t.TuKhoaTenDiaDiem))
                    result.Add(t.TuKhoaTenDiaDiem, t.TenDiaDiem1);
            }
            
            //for (int i = 0; i < query.Count; i++)
            //    if(null != query[i].TuKhoaTenDiaDiem && !result.ContainsKey(query[i].TuKhoaTenDiaDiem))
            //        result.Add(query[i].TuKhoaTenDiaDiem, query[i].TenDiaDiem1);

             return result;
        }
    }
}
